<?php
include_once('../../php/postgre.php');
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'save_bap') {
    $id_bdgbapeda = $_POST['id_bdgbapeda'];
    $nama_bdgbapeda = $_POST['nama_bdgbapeda'];

    if (!is_numeric($id_bdgbapeda)) {
        echo "Err: invalid id.";
        exit;
    }

    if ($id_bdgbapeda > 0) {
		$bdggg = get_data('select nama_bdgbapeda from mus_bidang_bappeda where id_bdgbapeda='.$id_bdgbapeda);
        exec_query("update mus_bidang_bappeda set nama_bdgbapeda='" . $nama_bdgbapeda . "' where id_bdgbapeda=" . $id_bdgbapeda);
        echo 'success__';
		logs('Mengubah nama bidang Bappeda','Dari "'.$bdggg['nama_bdgbappeda'].'" menjadi "'.$nama_bdgbapeda.'"');
    } else {
        $maxid = get_maxid('id_bdgbapeda', 'mus_bidang_bappeda');
        exec_query("insert into mus_bidang_bappeda(id_bdgbapeda, nama_bdgbapeda) values ('" . $maxid . "','" . $nama_bdgbapeda . "')");
        $stored = get_data('select id_bdgbapeda from mus_bidang_bappeda where id_bdgbapeda='.$maxid);
        if ($stored['id_bdgbapeda'] == $maxid) {
            echo 'success__';
            $id_bdgbapeda = $maxid;
        }
		logs('Menambah Bidang Bappeda','Nama Bidang "'.$nama_bdgbapeda.'"');
    }

    echo $id_bdgbapeda . '__';
    view_bidang();
} else if ($act == 'del_bap') {
    $id_bdgbapeda = $_POST['id_bdgbapeda'];
    if (is_numeric($id_bdgbapeda)) {
		$nmbid = get_data('select nama_bdgbapeda from mus_bidang_bappeda where id_bdgbapeda='.$id_bdgbapeda);
        exec_query("delete from mus_bidang_bappeda where id_bdgbapeda=" . $id_bdgbapeda);
        echo 'success__';
		view_bidang();
		
		logs('Menghapus bidang di Bappeda','Bidang yang dihapus adalah '.$nmbid['nama_bdgbapeda']);
    }
} else if ($act == 'edit_bap') {
    $id_bdgbapeda = $_POST['id_bdgbapeda'];
    if (is_numeric($id_bdgbapeda)) {
        $data = get_data("select * from mus_bidang_bappeda  where id_bdgbapeda =" . $id_bdgbapeda);
        $ress = implode($data, '__');
        //print_r($ress);
        echo $ress;
    } else {
        echo 'error';
    }
}

function view_bidang() {

    $x = 1;
    $bidang = get_datas("select * from mus_bidang_bappeda");
    foreach ($bidang as $bidang) {
        ?>
        <tr>
            <td class="center"><?php echo $x ?></td>
            <td><?php echo $bidang['nama_bdgbapeda']; ?></td>
            <td class="center">
                <a  href="#modalwin" data-toggle="modal"  class="btn btn-info btn-small bt-edit-bap" name="<?php echo $bidang['id_bdgbapeda']; ?>"><i class="icon-edit icon-white"></i> ubah</a>
                <a class="btn btn-danger btn-small bt-delete-bap" name="<?php echo $bidang['id_bdgbapeda']; ?>"><i class="icon-trash icon-white"></i> hapus</a>

            </td>
        </tr>
        <?php
        $x++;
    }
}
?>
